package com.syqc.monitor.ui.cost

import androidx.lifecycle.MutableLiveData
import com.syqc.comlib.utils.GsonUtil
import com.syqc.entity.TrafficInfoFee
import com.syqc.monitor.R
import com.syqc.utils.TimeUtil
import com.syqc.utils.ToolsUtil


class TrafficViewModel : BaseFeeViewModel() {
    var feeSpeed = ""
        set(value) {
            field = value
            sum()
        }
    var feeBridge = ""
        set(value) {
            field = value
            sum()
        }
    var feeStop = ""
        set(value) {
            field = value
            sum()
        }
    var feeWash = ""
        set(value) {
            field = value
            sum()
        }
    var feeOther = ""
        set(value) {
            field = value
            sum()
        }


    val feeTotalLive = MutableLiveData<String>()

    //提交时间
    var update = TimeUtil.instance.getNowTime(true)
    private var trafficEntity: String = ""
    private var trafficFee: String = ""
    override fun isParamsOk(): Boolean {
        if (carId.isEmpty()) {
            toast(R.string.hint_toast_choose_car)
            return false
        }

        if (
            feeSpeed.isEmpty() || feeBridge.isEmpty() || feeStop.isEmpty() ||
            feeWash.isEmpty() || feeOther.isEmpty()
        ) {
            toast(R.string.hint_toast_fee)
            return false
        }

        val entity = TrafficInfoFee(
            carId, update, remark,
            ToolsUtil.formatTo2F(feeTotal),
            feeBridge, feeSpeed, feeOther, feeStop, feeWash
        )
        trafficFee = GsonUtil.toJson(entity)
        if ("$trafficFee$photos" == trafficEntity && isSuccess) {
            toast(R.string.toast_repeat_same_data)
            return false
        }
        trafficEntity = "$trafficFee$photos"
        return true
    }

    /**
     * 求和
     *
     */
    private fun sum() {
        feeTotal = getDouble(feeSpeed) + getDouble(feeBridge) +
                getDouble(feeStop) + getDouble(feeWash) + getDouble(feeOther)
        feeTotalLive.postValue(ToolsUtil.formatTo2F(feeTotal))
    }

    /**
     * 提交维修
     *
     */
    fun uploadTraffic() {
        if (!isParamsOk()) return
        uploadFee("FeeTraffic", entity = trafficFee.replace("%", "%25"))
    }

    override fun clearData() {
        super.clearData()
        update = TimeUtil.instance.getNowTime(true)
    }

}